
import { createApp } from 'vue'
import App from './App.vue'

//引入UI框架 全局挂载
import Vant from 'vant'; 
import 'vant/lib/index.css';

//引入封装异步请求axios
import request from './services/request'

//引入路由
import router  from './routers/index'

//引入cookie插件
import VueCookie from 'vue3-cookies'

//引入自定义封装的工具箱
import tools from './services/tool'

const app = createApp(App)
    .use(router)
    .use(Vant)
    .use(request)
    .use(VueCookie)
    .use(tools)
    .mount('#app')

//路由守卫
//to 去哪个路由 from 从哪来 next 是否去到下一步
router.beforeEach(async (to,from)=>{
    var IsLogin = to.meta.IsLogin ? to.meta.IsLogin : false

    if(IsLogin)
    {
        var result = await app.$login()
        //查找用户失败
        if(result.code == 0)
        {
            //保留它所需去到的页面地址
            app.$cookies.set('redirect',to.fullPath);

            //清除缓存
            app.$cookies.remove('business')
 
            //提醒信息
            app.$fail(result.msg)

            //跳转回登录界面
            return {path: '/business/login'}
        }
    }
})

  